package com.web.surf.sample.eshop.database.dao.services;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.web.surf.sample.eshop.database.dao.interfaces.IFeaturesDao;
import com.web.surf.sample.eshop.faces.core.AbstractBean;

public class FeaturesDaoImpl extends AbstractDao implements IFeaturesDao {

	private static final String SQL_FEATURES_BYID = "SELECT f.feature_name, gf.feature_value FROM features AS f, goods_features AS gf "
			+ "WHERE gf.feature_id IN (SELECT feature_id FROM goods_features WHERE goods_id = ?) AND gf.feature_id = f.feature_id";

	public Map<String, String> getFeaturesByID(int goodsID) {
		try {
			PreparedStatement getFeaturesByID = getConnection()
					.prepareStatement(SQL_FEATURES_BYID);
			getFeaturesByID.setInt(1, goodsID);
			return (Map<String, String>) executeMapSelect(getFeaturesByID);

		} catch (SQLException e) {
			terminateConnection();
			return null;
		}
	}

	protected List<AbstractBean> processSelectResult(ResultSet rs)
			throws SQLException {
		return null;
	}

	protected Map<String, String> processMapSelect(ResultSet rs)
			throws SQLException {
		Map<String, String> map = new HashMap<String, String>();
		while (rs.next()) {
			map.put(rs.getString(1), rs.getString(2));
		}
		return map;
	}

	protected int processSelectCount(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}
}